package com.now.printer.utils;

import android.content.Context;
import android.print.PrintAttributes;
import android.print.PrintManager;

import com.now.printer.R;
import com.now.printer.print.MyPrintDocumentAdapter;
import com.now.printer.print.PDFCheck;
import com.xuexiang.xutil.tip.ToastUtils;

import org.xutils.common.util.LogUtil;

import java.io.File;

public class PrinterUtils {

    /**
     * 自定义打印
     */
    public static void doPrint(Context context, String filePath) {
        if (!PDFCheck.check(filePath)) {
            ToastUtils.toast("PDF文件加载错误");

            File file = new File(filePath);
            if (file.delete()) {
                LogUtil.i("即将删除pdf file is : " + filePath);
            }
            return;
        }
        PrintManager printManager = (PrintManager) context.getSystemService(Context.PRINT_SERVICE);
        String jobName = context.getString(R.string.app_name) + "Document";
        try {
            PrintAttributes attrib = new PrintAttributes.Builder()
//                    .setMediaSize(PrintAttributes.MediaSize.UNKNOWN_LANDSCAPE)
                    .setColorMode(PrintAttributes.COLOR_MODE_MONOCHROME)
                    .build();
            printManager.print(jobName, new MyPrintDocumentAdapter(context, filePath), attrib);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.e("doPrint Exception :" + e.getMessage());
        }
    }


}
